package com.service.Authorization.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.service.Authorization.dao.ManageProgrammeInfoDao;
import com.service.Authorization.dao.ProgrammeButtonDao;
import com.service.Authorization.dao.ProgrammePasswordLayoutDao;
import com.service.Authorization.dto.AssemblyInfoDto;
import com.service.Authorization.dto.ProgrammeButtonDto;
import com.service.Authorization.dto.ProgrammeInfoExamineDto;
import com.service.Authorization.dto.ProgrammePasswordLayoutDto;
import com.service.Authorization.entity.ManageProgrammeInfo;
import com.service.Authorization.entity.ProgrammeButton;
import com.service.Authorization.entity.ProgrammeInfoExamine;
import com.service.Authorization.entity.ProgrammePasswordLayout;
import com.service.Authorization.service.ManageProgrammeInfoService;
import com.service.Authorization.service.ProgrammeInfoExamineService;
import com.service.config.utils.JsonXMLUtils;
import com.service.config.utils.ModelMapperUtil;
import com.service.config.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 方案信息表(ManageProgrammeInfo)表服务实现类
 *
 * @author makejava
 * @since 2021-08-10 16:14:24
 */
@Service("manageProgrammeInfoService")
public class ManageProgrammeInfoServiceImpl implements ManageProgrammeInfoService {
    @Resource
    private ManageProgrammeInfoDao manageProgrammeInfoDao;
    @Resource
    private ProgrammePasswordLayoutDao programmePasswordLayoutDao;
    @Resource
    private ProgrammeButtonDao programmeButtonDao;
    @Resource
    private ProgrammeInfoExamineService programmeInfoExamineService;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ManageProgrammeInfo queryById(String id) {
        return this.manageProgrammeInfoDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ManageProgrammeInfo> queryAllByLimit(int offset, int limit) {
        return this.manageProgrammeInfoDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param manageProgrammeInfo 实例对象
     * @return 实例对象
     */
    @Override
    public ManageProgrammeInfo insert(ManageProgrammeInfo manageProgrammeInfo) {
        this.manageProgrammeInfoDao.insert(manageProgrammeInfo);
        return manageProgrammeInfo;
    }

    /**
     * 修改数据
     *
     * @param manageProgrammeInfo 实例对象
     * @return 实例对象
     */
    @Override
    public ManageProgrammeInfo update(ManageProgrammeInfo manageProgrammeInfo) {
        this.manageProgrammeInfoDao.update(manageProgrammeInfo);
        return this.queryById(manageProgrammeInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.manageProgrammeInfoDao.deleteById(id) > 0;
    }

    @Override
    public Result queryManageProgrammeInfo(String userId, String programmeInfoId) throws Exception {
        ManageProgrammeInfo manageProgrammeInfo = manageProgrammeInfoDao.queryById(programmeInfoId);
        if (manageProgrammeInfo != null) {
            List<ProgrammePasswordLayout> programmePasswordLayouts = new ArrayList<>();
            ProgrammeInfoExamineDto programmeInfoExamine = ModelMapperUtil.strictMap(manageProgrammeInfo, ProgrammeInfoExamineDto.class);
            programmeInfoExamine.setComment(JSONArray.parseArray(manageProgrammeInfo.getComment()).toJavaList(AssemblyInfoDto.class));
            ProgrammeButton programmeButton = new ProgrammeButton();
            programmeButton.setProgrammeId(programmeInfoExamine.getId());
            programmeButton.setManageId(userId);
            List<ProgrammeButton> programmeButtons = programmeButtonDao.queryAll(programmeButton);
            ProgrammeInfoExamine programmeInfoExamine2 = programmeInfoExamineService.queryById(programmeInfoExamine.getId());
            if (programmeInfoExamine2.getPassword()) {
                ProgrammePasswordLayout programmePasswordLayout = new ProgrammePasswordLayout();
                programmePasswordLayout.setProgrammeId(programmeInfoExamine.getId());
                programmePasswordLayout.setManageId(userId);
                programmePasswordLayouts = programmePasswordLayoutDao.queryAll(programmePasswordLayout);
                programmeInfoExamine.setProgrammePasswordLayout(JsonXMLUtils.json2obj(JsonXMLUtils.obj2json(programmePasswordLayouts.get(0)),ProgrammePasswordLayoutDto.class));
            }else {
                programmeInfoExamine.setPassword(false);
            }
            programmeInfoExamine.setProgrammeButton(JsonXMLUtils.json2obj(JsonXMLUtils.obj2json(programmeButtons.get(0)),ProgrammeButtonDto.class));
            return Result.ok().setData(programmeInfoExamine);
        }
        return Result.failure("方案不存在");
    }
}
